package com.smartEdu.websocket;

import java.io.IOException;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.Map;

import org.springframework.web.socket.TextMessage;  
import org.springframework.web.socket.WebSocketSession;
import org.springframework.web.socket.handler.TextWebSocketHandler;  

public class WebsocketEndPoint extends TextWebSocketHandler {  
  
	public static Map<String,WebsocketEndPoint> cowa=new HashMap<String,WebsocketEndPoint>();
	public WebSocketSession session=null;
    @Override  
    protected void handleTextMessage(WebSocketSession session,  
            TextMessage message) throws Exception {  
        super.handleTextMessage(session, message);  
        TextMessage returnMessage = new TextMessage(message.getPayload()+" received at server");  
        session.sendMessage(returnMessage);  
    }  
    
    @Override
    public void afterConnectionEstablished(WebSocketSession session)
            throws Exception{
    	super.afterConnectionEstablished(session);
    	cowa.put(session.toString(),this);
    	this.session=session;
    	TextMessage returnMessage=new TextMessage("qr="+URLEncoder.encode(session.toString(),"UTF-8"));
    	session.sendMessage(returnMessage);  
    	System.err.println("WebSocketSession ID:"+session.getId());
    	System.err.println("WebSocketSession:"+session.toString());
    }
    
    public void sendmessage(String msg) throws IOException{
    	TextMessage returnMessage=new TextMessage(msg);
    	this.session.sendMessage(returnMessage); 
    }
}
